package com.gly091020;

import com.gly091020.config.EnglishFightConfig;
import com.gly091020.enchantment.EnglishFightEnchantment;
import com.gly091020.util.*;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.impl.attachment.AttachmentRegistryImpl;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.UUID;

public class EnglishFight implements ModInitializer {
    public static final String ModID = "englishfight";
    public static final Logger LOGGER = LoggerFactory.getLogger(ModID);
    public static final UUID _5112151111121 = UUID.fromString("91bd580f-5f17-4e30-872f-2e480dd9a220");
    public static final AttachmentType<Object> ENGLISH_COMBO = AttachmentRegistryImpl.builder()
            .initializer(EnglishCombo::new)
            .buildAndRegister(new Identifier(ModID, "english_combo"));
    public static final AttachmentType<Object> ENGLISH_ANSWER = AttachmentRegistryImpl.builder()
            .initializer(EnglishAnswer::new)
            .buildAndRegister(new Identifier(ModID, "english_answer"));
    public static final AttachmentType<Object> ENGLISH_QUESTION = AttachmentRegistryImpl.builder()
        .initializer(EnglishFightQuestion::new)
        .buildAndRegister(new Identifier(ModID, "english_question"));
    public static final EnglishFightEnchantment ENGLISH_FIGHT = new EnglishFightEnchantment();
    public static EnglishFightConfig config;
    @Override
    public void onInitialize() {
        Registry.register(Registries.ENCHANTMENT, new Identifier(ModID, "english_fight"), ENGLISH_FIGHT);
        ServerPlayNetworking.registerGlobalReceiver(NetworkID.SEND_ANSWER, (minecraftServer, serverPlayerEntity, serverPlayNetworkHandler, packetByteBuf, packetSender) -> {
            int i = packetByteBuf.readInt();
            if(i != -1){
                if (serverPlayerEntity.getAttached(ENGLISH_ANSWER) == null){
                    serverPlayerEntity.setAttached(ENGLISH_ANSWER, new EnglishAnswer());
                }
                ((EnglishAnswer) Objects.requireNonNull(serverPlayerEntity.getAttached(ENGLISH_ANSWER))).setAnswer(i);
            }else{
                var q = EnglishFightFun.getRandomQuestion();
                serverPlayerEntity.setAttached(ENGLISH_QUESTION, q);
                ServerPlayNetworking.send(serverPlayerEntity, NetworkID.UPDATE_QUESTION, EnglishFightFun.questionToBuffer(q));
            }
        });
        ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> {
            for(World world: minecraftServer.getWorlds()){
                for(PlayerEntity player: world.getPlayers()){
                    if (player.getAttached(EnglishFight.ENGLISH_COMBO) == null){
                        player.setAttached(EnglishFight.ENGLISH_COMBO, new EnglishCombo());
                    }
                    if (((EnglishCombo) Objects.requireNonNull(player.getAttached(EnglishFight.ENGLISH_COMBO))).getCombo() != 0){
                        if(EnchantmentHelper.getLevel(ENGLISH_FIGHT, player.getMainHandStack()) == 0){
                            ((EnglishCombo) Objects.requireNonNull(player.getAttached(EnglishFight.ENGLISH_COMBO))).resetCombo();
                            var p = PacketByteBufs.create();
                            p.writeInt(0);
                            ServerPlayNetworking.send((ServerPlayerEntity) player, NetworkID.UPDATE_COMBO, p);
                        }
                    }
                }
            }
        });
        AutoConfig.register(EnglishFightConfig.class, GsonConfigSerializer::new);
        config = AutoConfig.getConfigHolder(EnglishFightConfig.class).getConfig();
        EnglishFightFun.initFiles();
        EnglishWordPractice.reloadWords();
    }
}
